local _, playerClass = UnitClass("player")
if playerClass ~= "HUNTER" then return end


HST = AceLibrary("AceAddon-2.0"):new("AceEvent-2.0", "AceConsole-2.0", "AceDB-2.0", "AceHook-2.1", "FuBarPlugin-2.0")
HST:RegisterDB("HSTdb")

HST.ScanTip = CreateFrame("GameTooltip", "HSTScanTip", nil, "GameTooltipTemplate")
HST.ScanTip:SetOwner(WorldFrame, "ANCHOR_NONE")

HST.L = AceLibrary("AceLocale-2.2"):new("HST")
local L = HST.L

-- FUBAR Stuff -----------------------------------------------------------------------------
HST.name = "HST"
HST.hasNoColor = true
HST.hasIcon = "Interface\\Icons\\Ability_UpgradeMoonglaive"
HST.defaultMinimapPosition = -30
HST.cannotDetachTooltip = true
HST.hideWithoutStandby = true
HST.castbarStart = false
--------------------------------------------------------------------------------------------

function HST:OnInitialize()
	self.OnMenuRequest = self.cmdtable
	self:RegisterChatCommand({"/HST", "/hst"}, self.cmdtable)
	self:RegisterChatCommand({"/hstcast1"}, self.StartCastNoMultiShot)
	self:RegisterChatCommand({"/hstcast2"}, self.StartCastMultiShot)
	self:RegisterDefaults("profile", self.defaults.profile)

	self.lastshot = GetTime()
	self.berserkValue = 0
	self.ShotSpells = HST.ShotSpells
	self.ChatTypes = {
		["SAY"] = true,
		["EMOTE"] = true,
		["YELL"] = true,
		["PARTY"] = true,
		["GUILD"] = true,
		["OFFICER"] = true,
		["RAID"] = true,
		["RAID_WARNING"] = true,
	}

	self:RegisterEvents()
	self:Init()
	self:OnProfileEnable()

	self:SystemMessage(L["Loaded. The hunt begins!"])
end

function HST:Init()
	self:BarInit()
	self:CastBarInit()
end

function HST:OnProfileEnable()
	self:OnBarProfileEnabled()
	self:OnCastBarProfileEnabled()
	self:BarToggleLock(true)
end

function HST:RegisterEvents()
	self:RegisterEvent("SPELLCAST_START")
	self:RegisterEvent("SPELLCAST_STOP")
	self:RegisterEvent("SPELLCAST_FAILED")
	self:RegisterEvent("SPELLCAST_INTERRUPTED")
	self:RegisterEvent("SPELLCAST_DELAYED")
	self:RegisterEvent("START_AUTOREPEAT_SPELL")
	self:RegisterEvent("STOP_AUTOREPEAT_SPELL")
	self:RegisterEvent("SPELLCAST_CHANNEL_START")
	self:RegisterEvent("SPELLCAST_CHANNEL_UPDATE")
	self:RegisterEvent("SPELLCAST_CHANNEL_STOP")
	self:RegisterEvent("ITEM_LOCK_CHANGED")
	self:RegisterEvent("UNIT_RANGEDDAMAGE")
	self:RegisterEvent("CHAT_MSG_SPELL_DAMAGESHIELDS_ON_SELF")
	self:RegisterEvent("PLAYER_CONTROL_LOST")
	self:RegisterEvent("PLAYER_ENTERING_WORLD")
	self:RegisterEvent("ZONE_CHANGED_NEW_AREA")
	self:RegisterEvent("CHAT_MSG_SPELL_PERIODIC_SELF_DAMAGE")
	self:RegisterEvent("UNIT_AURA")
	self:RegisterEvent("PLAYER_REGEN_DISABLED")
	self:RegisterEvent("PLAYER_REGEN_ENABLED")

	self:RegisterEvent("UI_ERROR_MESSAGE")
	
	self:Hook("CastSpell")
	self:Hook("CastSpellByName")
	self:Hook("UseAction")

	self:SendPlayerMovingEvent()
end

function HST:OnClick()
	self:BarToggleLock()
end

function HST:OnToggleLock()
	self:BarToggleLock()
end

function HST:SystemMessage(msg)
	DEFAULT_CHAT_FRAME:AddMessage("|cFFAAD372HST|cFFFFFFFF: " .. msg)
end

function HST:Announce(msg)
	local channel = strupper(HST.db.profile.channel)
	if self.ChatTypes[channel] then
		SendChatMessage(msg, channel)
	else
		id = GetChannelName(HST.db.profile.channel)
		if id then
			SendChatMessage(msg, "CHANNEL", nil, channel)
		end
	end
end

HST.cmdtable = {
	type = "group",
	args = {
		lock = {
			name = L["Lock"],
			desc = L["Lock Timer and Castbar into position"],
			type = "toggle",
			get = function() return HST.db.profile.locked end,
			set = function() HST:OnToggleLock() end,
			order = 1,
		},
		timer = {
			type = "group",
			name = L["Timer options"],
			desc = L["Timer options"],
			args = {
				height = {
					type = "range",
					name = L["Height"],
					desc = L["Height"],
					min = 5,
					max = 80,
					step = 1,
					get = function() return HST.db.profile.height end,
					set = function(v) HST.db.profile.height = v; HST:OnProfileEnable() end,
					order = 1,
				},
				width = {
					type = "range",
					name = L["Width"],
					desc = L["Width"],
					min = 20,
					max = 800,
					step = 10,
					get = function() return HST.db.profile.width end,
					set = function(v) HST.db.profile.width = v; HST:OnProfileEnable() end,
					order = 2,
				},
				texture = {
					type = "text",
					name = L["Bar Texture"],
					desc = L["Bar Texture"],
					usage = L["<texturename>"],
					get = function() return HST.db.profile.timertexture end,
					set = function(v) HST.db.profile.timertexture = v; HST:OnProfileEnable() end,
					order = 9,
				},
			},
			order = 5,
		},
		tranqoptions = {
			type = "group",
			name = L["Tranq options"],
			desc = L["Tranq options"],
			args = {
				enable = {
					type = "toggle",
					name = L["Tranq announce"],
					desc = L["Enable Tranquilizing Shot announce"],
					get = function() return HST.db.profile.tranq end,
					set = function() HST.db.profile.tranq = not HST.db.profile.tranq end,
					order = 1,
				},
				fail = {
					type = "toggle",
					name = L["Tranq fail announce"],
					desc = L["Enable failed Tranquilizing Shot announce"],
					get = function() return HST.db.profile.tranqfailed end,
					set = function() HST.db.profile.tranqfailed = not HST.db.profile.tranqfailed end,
					order = 2,
				},
				channel = {
					type = "text",
					name = L["Channel"],
					desc = L["Channel in which to announce"],
					usage = L["<channelname>"],
					get = function() return HST.db.profile.channel end,
					set = function(v) HST.db.profile.channel = v end,
					order = 3,
				},
				tranqmsg = {
					type = "text",
					name = L["Tranq Message"],
					desc = L["What to send to the channel"],
					usage = L["Use plain text and substitute the targets name with %t"],
					get = function() return HST.db.profile.tranqmsg end,
					set = function(v) HST.db.profile.tranqmsg = v end,
					order = 4,
				},
				tranqfailmsg = {
					type = "text",
					name = L["Tranq fail Message"],
					desc = L["What to send to the channel when tranq failed"],
					usage = L["<Message>"],
					get = function() return HST.db.profile.tranqfailmsg end,
					set = function(v) HST.db.profile.tranqfailmsg = v end,
					order = 5,
				},
			},
			order = 6,
		},
		reset = {
			type = "execute",
			name = L["Reset Settings"],
			desc = L["Reset Settings"],
			func = function() StaticPopup_Show("RESET_HST_PROFILE") end,
			order = 8,
		},
		ew = {
			name = "破甲虚弱覆盖监控",
			desc = "猎人8T2破甲虚弱效果的覆盖率监控, 战斗结束后密语报告战斗覆盖情况",
			type = "toggle",
			get = function() return HST.db.profile.ew end,
			set = function() HST:OnEWToggle(HST.db.profile.ew) end,
			order = 11
		}
	}
}